// **************************************************************
// Com ++ Framework by the C++ World
// Please report defects: mailto:compp@cplusplusworld.com
// Please visit: http://www.cplusplusworld.com/socketlibrary.html
// **************************************************************

#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "ComPlusPlus/ComPlusPlus"

class Test1 {

public:
  Test1() {}
  ~Test1() {std::cout << "Test1 OBJECT deleted \n"; } ;

};

int main( int argc, char * argv [] ) {

  try {
    compp::Signalling<int> Sig;

    int val=0;
    compp::SignallingMemberID ID, ID1;
    ID = Sig.Join();
    ID1 = Sig.Join();
    
    bool f = Sig.TryWait ( ID, val );
    std::cout << "TryWait: delivered " << f << "should be 0\n";

    Sig.Post ( ID1 , 56 );
    Sig.Wait ( ID, val );
    std::cout << "Wait: delivered " << val << "should be 56\n";

    Sig.Post(ID, 78 );
    f = Sig.TryWait ( ID, val );
    std::cout << "TryWait: delivered " << f << "should be 0\n";
    
    f = Sig.TryWait ( ID1, val );
    std::cout << "TryWait: delivered " << f << "should be 1 and " << val << " should be 78 \n";

    f = Sig.TryWait ( ID1, val );
    std::cout << "TryWait: delivered " << f << "should be 0\n";
    
    Sig.Leave ( ID  );
    Sig.Leave ( ID1 );

    compp::Signalling<Test1> Sig2;
    
    Test1 Dummy;
    ID = Sig2.Join();
    ID1 = Sig2.Join();

    Sig2.Post(ID, Dummy);
    std::cout << "Leave1\n";
    Sig2.Leave(ID);
    std::cout << "Leave2\n";
    Sig2.Leave(ID1);    

    std::cout << "Leave3\n";
  } catch ( syspp::ComException &e ) {
    std::cout << "Exception " << e.what () << "\n"; 
  }
  return 0;
}

